* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Table: Correlation of Distance and Socio-Economic Characteristics            *
* **************************************************************************** *
/*
** Purpose:    Create Table:
			   Correlation of Distance and Socio-Economic Characteristics

*/
********************************************************************************

		
	* Create and label the distance variables:


	use  "${Replicate_SocialSignals_dtaInter}/Endline_Distance_Analysis.dta", clear

	rename  newdist distance
	replace distance = 0         if distance<=0.5
	replace distance = 1         if distance>0.5&distance<=1.5
	replace distance = 2         if distance>1.5&distance<=2.5
	replace distance = 3         if distance>2.5&distance<=3.5
	replace distance = 4         if distance>3.5&distance<=4.5
	replace distance = 5         if distance>4.5&distance!=.

	tab     distance, gen(dist)
	lab var dist1 "Distance 0 mile"
	lab var dist2 "Distance 1 mile"
	lab var dist3 "Distance 2 miles"
	lab var dist4 "Distance 3 miles"
	lab var dist5 "Distance 4 miles"
	lab var dist6 "Distance 5 miles"

	summ    dist?

	** Code timely vaccine outcomes
	local a = 91

	foreach    var in bcg {
	  replace `var' = 0         if `var'==1&age_`var'>=`a'&age_`var'!=.
	  replace `var' = 0         if `var'==.&age_pikin>=`a'&age_pikin!=.
	  replace `var' = .         if age_pikin<`a'
		}


	local b = 122

	foreach    var in penta1 {
	  replace `var' = 0         if `var'==1&age_`var'>=`b'&age_`var'!=.
	  replace `var' = 0         if `var'==.&age_pikin>=`b'&age_pikin!=.
	  replace `var' = .         if age_pikin<`b'

	  replace `var' = 0         if bcg ==0&`var'==1
	}

	local c = 152

	foreach    var in penta2 {
	  replace `var' = 0         if `var'==1&age_`var'>=`c'&age_`var'!=.
	  replace `var' = 0         if `var'==.&age_pikin>=`c'&age_pikin!=.
	  replace `var' = .         if age_pikin<`c'

	  replace `var' = 0         if penta1 ==0&`var'==1
	}


	local d = 183

	foreach    var in penta3 {
	  replace `var' = 0         if `var'==1&age_`var'>=`d'&age_`var'!=.
	  replace `var' = 0         if `var'==.&age_pikin>=`d'&age_pikin!=. 
	  replace `var' = .         if age_pikin<`d'

	  replace `var' = 0         if penta2 ==0&`var'==1
	}


	local e = 350

	foreach    var in measles1 {
	  replace `var' = 0         if `var'==1&age_`var'>=`e'&age_`var'!=.
	  replace `var' = 0         if `var'==.&age_pikin>=`e'&age_pikin!=.
	  replace `var' = .         if age_pikin<`e'

	  replace `var' = 0         if penta3 ==0&`var'==1
	}



	* --------------------------------------------------------------------------
	* Demean control variables
	tab     arm_anc, gen(anc)

	foreach control in anc2 anc3 anc4  {
		sum  `control', detail
		gen  `control'_dm     = `control' - `r(mean)'
	}

	foreach    var in age_pikin mother_age floor roof schooling farm trading birth_order {
	  summ    `var', detail
	  replace `var' = `r(mean)'         if `var'==.
	 }


	* label variables:
	lab var age_pikin    "Child age"
	lab var mother_age   "Mother age"
	lab var floor        "Floor cement"
	lab var roof         "Roof corrugated iron"
	lab var schooling    "Has any education"
	lab var farm         "Works on farm"
	lab var trading      "Trader"
	lab var birth_order  "Birth order"


	* --------------------------------------------------------------------------
	* Correlation of Distance and Socio-Economic Characteristics

	local  ANC_dm      = " anc2_dm anc3_dm anc4_dm "

	* a) Without controls:
	eststo clear
	foreach vaccine in penta2 penta3 measles1 {
		areg `vaccine' dist2 dist3 dist4 dist5 dist6   `ANC_dm', absorb(strata) cluster(clinic)
		estadd scalar C_mean        = _b[_cons]
		estadd local Obs            = "`e(N)'"
		eststo model`vaccine'
	}

	* b) With controls:
	foreach vaccine in penta2 penta3 measles1 {
		areg `vaccine' dist2 dist3 dist4 dist5 dist6   age_pikin birth_order mother_age floor roof schooling farm trading  `ANC_dm', absorb(strata) cluster(clinic)
		estadd scalar C_mean        = _b[_cons]
		estadd local Obs            = "`e(N)'"
		eststo model`vaccine'2
	}

	esttab  ///
	modelpenta2  modelpenta3  modelmeasles1    ///
	modelpenta22 modelpenta32 modelmeasles12   ///
	using "${Replicate_SocialSignals_ATables}/Table_Correlations_new.tex", ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule "                       ///
	"\multicolumn{1}{c}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} \\") ///
	scalars("C_mean  Outcome Mean"                                      ///
	"Obs     Observations")                                     ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	drop(*_dm) nomtitles                                                ///
	addnotes("") label se  nobaselevels noconstant noobs nolines        ///
	posthead("\midrule") postfoot("\bottomrule \end{tabular}") tex      ///
	replace

	filefilter "${Replicate_SocialSignals_ATables}/Table_Correlations_new.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_Correlations_Distance_SocioDemographics.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_Correlations_new.tex"




* **************************************************************************** *
* End of the Do file !!!
* **************************************************************************** *
